package hz.mobileproject.pda.presenter;

import android.content.Context;

import com.google.gson.Gson;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import hz.mobileproject.pda.entity.request.AllTalk;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.response.Response_data;
import hz.mobileproject.pda.network.Api;
import hz.mobileproject.pda.ui.activity.AllTalkActivity;
import hz.mobileproject.pda.utils.Base64;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by zmybi on 2017/8/24.
 */

public class AllTalkp extends XPresent<AllTalkActivity> {
    private final String token;
    /** 带String的实体类*/
    private Request_data data;
    /** 字符串*/
    private String body_data;
    /** okhttp3的请求体*/
    private RequestBody body;

    public AllTalkp() {
        data = new Request_data();
        this.token = getV().getApplicationContext().getSharedPreferences("app_login", Context.MODE_PRIVATE).getString("token","");
    }


    public void setData(AllTalk allTalk, int i) {
        Gson gson = new Gson();

        String json = gson.toJson(allTalk);
        //加密
        String mis_json = Base64.getBase64(json);
        data.setData(mis_json);
        body_data = new Gson().toJson(data);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body_data);

        useXMPByBody(requestBody,i);


    }

    private void useXMPByBody(RequestBody requestBody,int type) {
        switch (type) {
            case 1: {
                Api.getGankService().getAllTalk(requestBody,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {

                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                String data = response_data.getData();
                                getV().setView(Base64.getFromBase64(data),1);
                            }
                        });
            }
            break;
            case 2:  {   //下拉加载更多
                Api.getGankService().getAllTalk(requestBody,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {

                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                String data = response_data.getData();
                                getV().setView(Base64.getFromBase64(data),2);
                            }
                        });

            }
            break;
            case 3 : {
                Api.getGankService().getAllTalk(requestBody,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {

                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                String data = response_data.getData();
                                getV().setView(Base64.getFromBase64(data),3);
                            }
                        });
            }
            break;

        }

    }
}
